package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonMsg;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@RequestMapping("destination")
@Controller
public class DestinationController {

    @Autowired
    private IDestinationService destinationService;


    /**
     * 页面展示
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") DestinationQueryObject qo){
        //查询页面数据
        Page page = destinationService.query(qo);
        //分享数据到页面
        model.addAttribute("page", page);
        //导航吐司toasts
        List<Destination> toasts= destinationService.checkParent(qo.getParentId());
        //分享数据到页面
        model.addAttribute("toasts", toasts);
        return "destination/list";
    }

    /**
     * 编辑
     * @param id,info
     * @return
     */
    @RequestMapping("/updateInfo")
    @ResponseBody
    public Object updateInfo(String id, String info){

        destinationService.updateInfo(id,info);
        return new JsonMsg();
    }


    /**
     * 删除
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        destinationService.delete(id);
        return JsonResult.success();
    }

}
